package com.easybuy.web.controller.manager;


import com.easybuy.bean.Count;
import com.easybuy.bean.EasybuyOrder;
import com.easybuy.service.IOrderService;
import com.easybuy.service.impl.OrderServiaceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    IOrderService orderServiace;
    @RequestMapping("/listByPage")
    public ModelAndView listByPage(Model model,@RequestParam(value = "page",defaultValue = "1") Integer page,@RequestParam(value = "pageSize",defaultValue = "3")Integer pageSize){

        List<EasybuyOrder> orders=orderServiace.listByPage(page,pageSize);
        ModelAndView mv=new ModelAndView();
        Count row=orderServiace.countRow();

        int maxPage=row.getRow()%pageSize==0?row.getRow()/pageSize:row.getRow()/pageSize+1;
        System.out.println(maxPage);
        model.addAttribute("page",page);
        model.addAttribute("maxPage",maxPage);
        mv.addObject("orders",orders);
        mv.setViewName("manage/order");
        return mv;
    }

    @RequestMapping("/find")
    public ModelAndView find(@RequestParam(value = "orderId") Integer  orderId, @RequestParam(value = "userName")String userName){
        ModelAndView mv=new ModelAndView();
        System.out.println(orderId+"   "+userName);

        List<EasybuyOrder> orders=orderServiace.listByFind(orderId,userName);

        mv.addObject("orders",orders);
        mv.setViewName("manage/order");
        return mv;
    }


    @RequestMapping("/to_save")
    public String toSave(){
        return "manage/order-add";
    }

    @RequestMapping("/to_saveCommit")
    public String toSaveCommit(HttpServletRequest request){
        int eoId= Integer.parseInt(request.getParameter("eoId"));
        String eoUserId=request.getParameter("eoUserId");
        String eoUserName=request.getParameter("eoUserName");
        String eoUserAddress =request.getParameter("eoUserAddress");
        String year=request.getParameter("year");
        String month=request.getParameter("month");
        String day=request.getParameter("day");
        String data=year+"-"+month+"-"+day;
        BigDecimal cost= BigDecimal.valueOf(Integer.parseInt(request.getParameter("cost")));
        int  status= 1;
        int  type= Integer.parseInt(request.getParameter("type"));
        EasybuyOrder easybuyOrder=new EasybuyOrder(eoId,eoUserId,eoUserName,eoUserAddress,data,cost,status,type);
        int result=orderServiace.save(easybuyOrder);
        if(result<=0){
            //TODO 要么return 要吗抛出异常
        }
        return "manage/manage-result";
    }
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") int id){
        int result=orderServiace.delete(id);
        if(result==0){
         //TODO 错误信息 或者 抛出异常
        }
        return "manage/manage-result";
    }

    @RequestMapping("/edit")
    public String edit(Model model,int eoId){
        EasybuyOrder easybuyOrder=orderServiace.find(eoId);
        model.addAttribute("easybuyOrder",easybuyOrder);
        return "manage/order-modify";
    }

    @RequestMapping("/editSubmit/{id}")
    public String editSubmit(@PathVariable("id") int id,HttpServletRequest request){
        String eoUserId=request.getParameter("eoUserId");
        String eoUserName=request.getParameter("eoUserName");
        String eoUserAddress =request.getParameter("eoUserAddress");
        String eoCreateTime =request.getParameter("eoCreateTime");
        BigDecimal eoCost= BigDecimal.valueOf(Integer.parseInt(request.getParameter("eoCost")));
        int  eoStatus= Integer.parseInt(request.getParameter("eoStatus"));
        int  eoType= Integer.parseInt(request.getParameter("eoType"));
        EasybuyOrder order=new EasybuyOrder();
        order.setEoId(id);
        order.setEoUserName(eoUserName);
        order.setEoUserId(eoUserId);
        order.setEoUserAddress(eoUserAddress);
        order.setEoCreateTime(eoCreateTime);
        order.setEoCost(eoCost);
        order.setEoStatus(eoStatus);
        order.setEoType(eoType);
        int result=orderServiace.updateById(order);
        System.out.println(order);
        if(result<=0){
            return "manage/error";
        }
        return "manage/manage-result";
    }

}
